package bp.ccoa.httphandler;

import bp.da.DataRow;
import bp.da.DataTable;
import bp.da.DataType;
import bp.en.TSEntityNoName;
import bp.sys.SFSearch;
import bp.web.WebUser;

import java.util.*;

/**
 * 处理CCOA接口
 */
public class CCOA_Handler extends bp.difference.handler.DirectoryPageBase {

    /**
     * 日历数据源初始化
     *
     * @return
     * @throws Exception
     */
    public final String CalendarDBSrc_Init() throws Exception {
        String ids = this.GetRequestVal("Ids");
        if (DataType.IsNullOrEmpty(ids)) {
            return bp.tools.Json.ToJson(new DataTable());
        }
        String nianye = this.GetRequestVal("NY");
        if (nianye == null) {
            nianye = DataType.getCurrentAP();
        }

        DataTable dt = new DataTable();
        dt.Columns.Add("Title");
        dt.Columns.Add("Url");
        dt.Columns.Add("Docs");
        dt.Columns.Add("RDT");
        dt.Columns.Add("SrcNo");
        dt.Columns.Add("SrcName");

        Hashtable ht = new Hashtable();
        ht.put("UserNo", WebUser.getNo());
        ht.put("UserName", WebUser.getName());
        ht.put("NY", nianye);

        String[] strs = ids.split(",");
        for (String str : strs) {
            TSEntityNoName en = new TSEntityNoName("TS.CCOA.CalendarDBSrc", str);

            String sfSearchNo = en.GetValStrByKey("SearchNo");

            SFSearch search = new SFSearch(sfSearchNo);
            DataTable tempDT = search.GenerHisDataTable(ht);

            for (DataRow dr : tempDT.Rows) {
                DataRow mydr = dt.NewRow();
                mydr.setValue("SrcNo", search.GetValStrByKey("No"));
                mydr.setValue("SrcName", search.GetValStrByKey("Name"));

                if (tempDT.Columns.contains("Title")) {
                    mydr.setValue("Title", dr.getValue("Title"));
                }
                if (tempDT.Columns.contains("Url")) {
                    mydr.setValue("Url", dr.getValue("Url"));
                }
                if (tempDT.Columns.contains("Docs")) {
                    mydr.setValue("Docs", dr.getValue("Docs"));
                }
                if (tempDT.Columns.contains("RDT")) {
                    mydr.setValue("RDT", dr.getValue("RDT"));
                }
                dt.Rows.add(mydr);
            }
        }
        return bp.tools.Json.ToJson(dt);
    }

}
